package com.yulebaby.callsenter.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

import com.yulebaby.callsenter.global.App;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class CommonUtil {

    /**
     * 获取资源文件中的文字信息
     *
     * @param resId
     * @return
     */
    public static String getString(int resId) {
        return App.mContext.getResources().getString(resId);
    }

    /**
     * 获取资源文件中的文字数组信息
     *
     * @param resId
     * @return
     */

    public static String[] getStringArray(int resId) {
        return App.mContext.getResources().getStringArray(resId);
    }

    /**
     * 获取资源中的px
     *
     * @param resId
     * @return
     */
    public static int getDimens(int resId) {
        return App.mContext.getResources().getDimensionPixelSize(resId);
    }

    /**
     * 获取资源中颜色值
     *
     * @param resId
     * @return
     */
    public static int getColor(int resId) {
        return App.mContext.getResources().getColor(resId);
    }

    /**
     * android4.4以上版本获取真实的相册图片路径
     *
     * @param context
     * @param uri
     * @return
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static String getRealPathFromURI_API19(Context context, Uri uri) {
        String filePath = "";
        try {

            String wholeID = DocumentsContract.getDocumentId(uri);

            // Split at colon, use second item in the array
            String id = wholeID.split(":")[1];

            String[] column = { MediaStore.Images.Media.DATA };

            // where id is equal to
            String sel = MediaStore.Images.Media._ID + "=?";

            Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[] { id }, null);

            int columnIndex = cursor.getColumnIndex(column[0]);

            if (cursor.moveToFirst()) {
                filePath = cursor.getString(columnIndex);
            }
            cursor.close();
        }
        catch (Exception e) {
            if (uri.getScheme().equals("file")) {
                return uri.getPath();
            }
        }

        return filePath;
    }

}
